﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HelpCentral.Configuration;
using HelpCentral.Shared.Interfaces;
using HelpCentral.Core.Objects;
using HelpCentral.Data.Model;
using HelpCentral.Data.Repository;
using HelpCentral.Shared.Enums;

namespace HelpCentral.Core.Services
{
    public class AuditService
    {
        public AuditService()
        {
            //CoreContainer.Register<IClient, ClientInfo>();
            CoreContainer.Register<IAuditEvent, AuditEventInfo>();
        }

        protected IQueryable<AuditEvent> AllAuditEvents(HelpCentralModelContainer context)
        {
            return context.AuditEvents
                    .Include("User")
                    .Include("Client");
        }

        //public List<IAuditEvent> AuditEventsByClientId(int ClientId)
        //{
        //    using (var context = new HelpCentralModelContainer())
        //    {
        //        return AllAuditEvents(context).Where(x => x.Client.ClientId == ClientId).OrderBy(x => x.Date).Select(x => x.FromRepositoryObject()).ToList();
        //    }
        //}

        public void SaveAuditEvent(IAuditEvent e)
        {
            e.Validate();

            using (var context = new HelpCentralModelContainer())
            {
                var ar = new BaseRepository<AuditEvent>(context);
                var a = ar.FirstOrDefault(x => x.AuditId == e.AuditId);
                bool createNew = false;

                if (a == null)
                {
                    createNew = true;
                    a = new AuditEvent();
                }

                a.AffectedObjectId = e.AffectedObjectId;
                a.SecondAffectedObjectId = e.SecondAffectedObjectId;

                if(e.Client != null) 
                {
                    a.Client = context.Clients.FirstOrDefault(x => x.ClientId == e.Client.ClientId);

                    if (a.Client == null)
                    {
                        //a.Client = e.Client.ToRepositoryObject(context);
                    }
                }

                a.Date = e.Date;
                a.EventType = e.EventType.ToString();
                a.Information = e.Information;

                if (createNew)
                {
                    ar.Add(a);
                }

                context.SaveChanges();
            }
        }

        public void SaveAuditEvent(AuditEventType eventType, IClient client, string information)
        {
            SaveAuditEvent(eventType, client, information, null, null);
        }

        public void SaveAuditEvent(AuditEventType eventType, IClient client, string information, int? affectedObjectId)
        {
            SaveAuditEvent(eventType, client, information, affectedObjectId, null);
        }

        public void SaveAuditEvent(AuditEventType eventType, IClient client, string information, int? affectedObjectId, int? secondAffectedObjectId)
        {
            var ar = CoreContainer.ResolveNew<IAuditEvent>();

            ar.Client = client;
            ar.Date = DateTime.UtcNow;
            ar.EventType = eventType;
            ar.Information = information;
            ar.AffectedObjectId = affectedObjectId;
            ar.SecondAffectedObjectId = secondAffectedObjectId;

            SaveAuditEvent(ar);
        }
    }
}
